define(['view', './config'], function(View, Config) {
  return View.extend({

    className: 'shadow',

    templatePath: 'create-package.html',

    events: {
      'click #cancelBtn': 'hide',
      'click #createBtn': 'createPackage'
    },

    init: function() {

    },

    onShow: function() {
     
    },

    hide: function() {
      var _this = this;
      this.$('.detail-wrapper').toggleClass('zoomIn zoomOut');
      this.$el.toggleClass('animated fadeOut');
      $('[pure-content]').css('overflow', 'scroll');
      setTimeout(function() {
        View.prototype.hide.call(_this);
      }, 200)
    },

    createPackage: function() {
      var name = this.$('#packageName').val();
      var author = this.$('#author').val();
      var gitRepo = this.$('#packageGit').val();
      var desc = this.$('#packageDesc').val();

      var formData = new FormData();

      var _this = this;
      var data = {
        name: name,
        repository_url: gitRepo,
        description: desc || 'put your description in this place',
        author: author || 'chameleon'
      };

      _.each(data, function(value, key) {
        formData.append(key, value);
      });
      
      $.ajax({
        type: 'POST',
        url: Config.server_url + '/packages?access_token=123',
        data: formData,
        contentType: false,
        processData: false,
        success: function(data) {
          if(data.code == 201) {
            _this.trigger('create');
            _this.hide();
          }
        }
      })
    }

    
  });
})